package net.xdclass.online_xdclass.service.impl;

import net.xdclass.online_xdclass.config.CacheKeyManager;
import net.xdclass.online_xdclass.model.entity.Video;
import net.xdclass.online_xdclass.model.entity.VideoBanner;
import net.xdclass.online_xdclass.mapper.VideoMapper;
import net.xdclass.online_xdclass.service.VideoService;
import net.xdclass.online_xdclass.utils.BaseCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
  private   BaseCache  baseCache;

    @Override
//    简单的查询
    public List<Video> listVideo() {

        try {
            Object videoObject  =baseCache.getTenMinuteCache().get(CacheKeyManager.INDEX_VIDEO_KRY,()->{
                List<Video> videoList=  videoMapper.listVideo();
                return videoList;
            });
            if (videoObject instanceof List){
//                从古瓦里面取的变成了Object的 所以要强转
                List<Video> videoList= (List<Video>)videoObject;
                return videoList;
            }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

//    视频列表
    @Override
    public Video findDetailById(int videoId) {
//String.format
        String videoCache= String.format(CacheKeyManager.INDEX_DETAIL_KEY,videoId);
        try {
           Object detailObject = baseCache.getTenMinuteCache().get(videoCache,()->{
                Video video= videoMapper.findDetailById(videoId);

                return video;

            });
           if (detailObject instanceof Video){
               Video video=(Video)detailObject;
               return video;
           }
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Video findVideo(Integer videoId) {
        return null;
    }


    //轮播图地址
    @Override
    public List<VideoBanner> listVideoBanner() {
//        callable是个回调函数的意思  首先在这里找找不到就去数据库里面取
        try{
//      第一个参数就是首先在古瓦里面找到就把值传给objects 找不到就去数据库里面找就是在lambda表达式那些语句
           Object objects=baseCache.getTenMinuteCache().get(CacheKeyManager.INDEX_BANNER_KEY,()->{
                List<VideoBanner> bannerList= videoMapper.listVideoBanner();

                return bannerList;  }
            );
//

            if(objects instanceof List){
                List<VideoBanner> bannerList=(List<VideoBanner>)objects;

                return bannerList;
}
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
